import org.junit.Test;

/**
 * @author jinhu
 * created 2021-05-23 20:07
 */
public class Wordbiz {
    //存结果
 public  WordStructer[] res = new WordStructer[100000];
    class WordStructer {
        public char zifu;
        public int count;

        public WordStructer(char zifu, int count) {
            this.zifu = zifu;
            this.count = count;
        }
    }

    /**
     * 字符串压缩
     *
     * @param S
     * @return
     */
    public String compressString(String S) {
        if (S == null || S.length() == 0) {
            return null;
        }
        res[0]=new WordStructer('*',0);
        int j = 0;//表示结果数组的长度
        char[] chars = S.toCharArray();
        for (int i = 0; i < chars.length;i++ ) {
            if (res[j].zifu == chars[i]) {
                res[j].count++;
            } else {
                res[j+1] = new WordStructer(chars[i],1);
                j++;
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= j; i++) {
            stringBuffer.append(res[i].zifu).append(res[i].count);
        }
        String result=stringBuffer.toString();

        return result.length() > S.length() ? S : result;

    }

    @Test
    public void test() {
//        char a = 'a';
//        System.out.println((int) a);
//        System.out.println((int) 'z');
//        char A = 'A';
//        System.out.println((int) A);
//        System.out.println((int) 'Z');
//        WordStructer[] res = new WordStructer[100000];
//        for (int i = 0; i < 3; i++) {
//            System.out.println(res[i]);
//        }
//        System.out.println("".length());
        String test="aaavvbbzzaahhyyyyzzzyyyy";
        System.out.println(this.compressString(test));
         test="aabcvy";
        System.out.println(this.compressString(test));
        System.out.println((int) '*');

    }
}
